package com.wuxiaoqin.admin.config.security;

import com.wuxiaoqin.admin.entity.Ad;
import com.wuxiaoqin.admin.entity.SecurityAd;


import com.wuxiaoqin.admin.service.IAdService;
import com.wuxiaoqin.securityConfig.security.JwtAuthencationTokenFilter;
import com.wuxiaoqin.securityConfig.security.RestAuthorizationEntryPoint;
import com.wuxiaoqin.securityConfig.security.RestfulAccessDeniedHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

/**
 * Security配置类
 *
 * @author zhoubin
 * @since 1.0.0
 */
@Configuration
//	@Deprecated
public class SecurityConfig extends WebSecurityConfigurerAdapter {
	@Autowired
	private IAdService iAdService;
	@Autowired
	private RestAuthorizationEntryPoint restAuthorizationEntryPoint;
	@Autowired
	private RestfulAccessDeniedHandler restfulAccessDeniedHandler;


	@Override
	protected void configure(AuthenticationManagerBuilder auth) throws Exception {
		auth.userDetailsService(userDetailsService()).passwordEncoder(passwordEncoder());
	}

	@Override
	public void configure(WebSecurity web) throws Exception {

		web.ignoring().antMatchers(
				//"/**",
				"/doc.html",
				"/getimgpath",
				"/staticpic/**",
				"/index/**",
				"/ad/login",
				"/static/img/**",
				"/staticimg/**",
				"/img/**",
				"/img/index.html",
				"/logout",
				"/css/**",
				"/js/**",
				"/static/index.html",
				"favicon.ico",
				"/swagger-ui/**",
				"/swagger-ui.html",
				"/webjars/**",
				"/swagger-resources/**",
				"/v2/**",
				"/v3/**",
				"/api/**",
				"/captcha",
				"/ws/**"

		);
	}

	@Override
	protected void configure(HttpSecurity http) throws Exception {
		//使用JWT，不需要csrf
		http.csrf()
				.disable()
				//基于token，不需要session
				.sessionManagement()
				.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
				.and()
				.authorizeRequests()
				//除了上面所有请求都要认证
				.antMatchers(HttpMethod.OPTIONS, "/**").permitAll()
				.anyRequest()
				.authenticated()
				//.antMatchers().hasRole("user")
				.and()
				//禁用缓存
				.headers()
				.cacheControl()
				;
		//添加jwt 登录授权过滤器
		http.addFilterBefore(jwtAuthencationTokenFilter(), UsernamePasswordAuthenticationFilter.class);
		//添加自定义未授权和未登录结果返回
		http.exceptionHandling()
				.accessDeniedHandler(restfulAccessDeniedHandler)
				.authenticationEntryPoint(restAuthorizationEntryPoint);
	}
@Override
@Bean
public UserDetailsService userDetailsService(){
	return aId -> {
		Ad ad = iAdService.getUsersByAdminId(aId);
		if (null!=ad){
			SecurityAd securityAd = new SecurityAd();
			securityAd.setCurrentUserInfo(ad);
			return securityAd;
		}
		throw new UsernameNotFoundException("用户名或密码不正确");
	};
}

	@Bean
	public PasswordEncoder passwordEncoder(){
		return new BCryptPasswordEncoder();
	}

	@Bean
	public JwtAuthencationTokenFilter jwtAuthencationTokenFilter(){
		return new JwtAuthencationTokenFilter();
	}

}
